#!/usr/bin/python

#Created by turk 2016-06-14
#Check TOOL [PYTHON]
#Version 1.0 

import sys
import os
import re
import time
import logging
import ConfigParser

LOGFILE = '/home/bsmp/work/log/checktoollog.log'

#Logger
logging.basicConfig(level=logging.DEBUG,  
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',  
                    datefmt='%Y-%m-%d %H:%M:%S',  
                    filename=LOGFILE,  
                    filemode='w')  
console = logging.StreamHandler()  
console.setLevel(logging.DEBUG)  
# set print format
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')  
console.setFormatter(formatter)  
#
logging.getLogger('').addHandler(console)  
logger = logging.getLogger('check')    


def checkDisk(disk):  
    #checkDisk space
    #set dir
    vfs=os.statvfs(disk)
    
    #free
    available=vfs[statvfs.F_BAVAIL]*vfs[statvfs.F_BSIZE]/(1024*1024*1024)
    print 'Disk [%s] has free space [%s G]' % (dir, available)

def checkDirsize(dir):
   size = 0L
   for root, dirs, files in os.walk(dir):
      size += sum([getsize(join(root, name)) for name in files])
   #return size
   size = size/(1024*1024*1024)
   print 'Dir [%s] used [%s (G)]' % (dir, size)    


def main ():
    
    if len(sys.argv) <> 4:
        print "Usage:<HOSTFILE> <USERNAME> <PASSWORD> <COMMAND>"
        return
    
    #read ini config file
    config_file_path = '/root/config.ini'
    cf = ConfigParser.ConfigParser()
    cf.read(config_file_path)
    logger.debug('check starting ...')
    
    s = cf.sections()
    print 'section:', s
    o = cf.options("baseconf")
    print 'options:', o
    v = cf.items("baseconf")
    print 'db:', v
    
    #section items
    db_host = cf.get("baseconf", "host")
    db_port = cf.getint("baseconf", "port")
    db_user = cf.get("baseconf", "user")
    db_pwd = cf.get("baseconf", "password")
        
    
    
    #ltime = time.time()
    #ftime = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))
    #bak_name = putfile + '_' + ftime
    #os.rename(putfile, bak_name)

    logger.debug('scp end ...')
    return

if __name__ == '__main__':
    # start 
    try:
        main()
    except Exception, e:
        print str(e)
        #traceback.print_exc()
        os._exit(1)

